<?php
/**
 * 
 * 
 * @author AnthonyYang
 */
class JDbConnection extends CDbConnection
{
	const ConnectionComponent = 'CDbConnection';

	/**
	 * @var array
	 * 
	 * @access public
	 */
	public $multi = array();
	
	/**
	 * @var array
	 *
	 * @access public
	*/
	public $map = array();
	
	/**
	 * @var array
	 *
	 * @access protected
	*/
	protected $connection = array();
	
	/**
	 *
	 *
	 * @author AnthonyYang
	 * @access public
	 * @return boolean
	*/
	public function hasConnection ($name)
	{
		try {
			return ($this->getConnection($name) instanceof CDbConnection);
		} catch (Exception $e) {
			return false;
		}
	}
	
	/**
	 *
	 *
	 * @author AnthonyYang
	 * @access public
	 * @return CDbConnection
	 */
	public function getConnection ($name)
	{
		$name = strtolower($name);
		if (isset($this->map[$name])) {
			$name = $this->map[$name];
		}
		if (!isset($this->multi[$name])) {
			return $this;
		}
		if (isset($this->connection[$name]) && $this->connection[$name] instanceof CDbConnection) {
			return $this->connection[$name];
		}
		$this->multi[$name]['class'] = isset($this->multi[$name]['class'])?$this->multi[$name]['class']:self::ConnectionComponent;
		$this->connection[$name] = Jii::createComponent($this->multi[$name]);
		return $this->connection[$name];
	}
}